package com.jiaai.cloud.wx.controller;

import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.wx.entity.TAdmin;
import com.jiaai.cloud.wx.entity.TGroup;
import com.jiaai.cloud.wx.entity.TPay;
import com.jiaai.cloud.wx.service.ITGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-28
 */
@Controller
@RequestMapping("/api/v1/tgroup")
public class TGroupController {

    @Autowired
    private ITGroupService itGroupService;

    //todo tested
    /**
     * 查询所有分组
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/queryall",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryAll()  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itGroupService.selectList(null));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 查询单个分组
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/queryone/{id}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryOne(@PathVariable("id") Integer id)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itGroupService.selectById(id));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 添加分组
     * @param group
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object add(@RequestBody TGroup group)  throws ServiceException {
        itGroupService.insert(group);
        try {
            return RestResultGenerator.genSuccessResult("添加分组成功");
        }catch (Exception e) {
            throw new ServiceException("添加分组失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 更新分组
     * @param tGroup
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@RequestBody TGroup tGroup)  throws ServiceException {
        try {
            itGroupService.updateById(tGroup);
            return RestResultGenerator.genSuccessResult("更新分组成功");
        }catch (Exception e) {
            throw new ServiceException("更新分组失败", e.getMessage());
        }
    }
}
